#include "HCSettings.h"
#include <layout.h>

#include "ui_HCSettings.h"
#include <QX11Info>
#include <QGroupBox>
#include <qformlayout.h>

#include "HCINDI.h"

HCSettings::HCSettings(): QDialog()
{
  ui = new Ui::HCSettings();
  display = QX11Info::display();
  lm = new LayoutManager(false);
  lm->updateJoyDevs();
  lm->load();
  labelnojoypad=new QLabel("No joystick devices are currently available to configure."
       "Please plug in a gaming device and press \"Update Joystick Devices\".");
  labelnojoypad->setWordWrap(true);   

}

void HCSettings::setupUi()
{
  ui->setupUi(this);
  ui->tabWidgetSettings->setTabText(0, "Devices");
  ui->tabWidgetSettings->setTabText(1, "JoyPad");
  
  lineEdithostname = new QLineEdit(hApp->getHandController()->getServerName());
  lineEdithostport = new QLineEdit(hApp->getHandController()->getPortString());
  lineEditmountname = new QLineEdit(hApp->getHandController()->getMountName());
  
  QVBoxLayout *vdevices = new QVBoxLayout();
  
  QGroupBox *qb = new QGroupBox("INDI Server");
  QFormLayout *qf = new QFormLayout(qb);
  QLabel *ql = new QLabel("Host name:");
  qf->addRow(ql, lineEdithostname);
  ql=new QLabel("Host Port:");
  qf->addRow(ql, lineEdithostport);
  
  vdevices->addWidget(qb);
  
  qb = new QGroupBox("Mount Device");
  qf = new QFormLayout(qb);
  ql=new QLabel("Mount Name:");
  qf->addRow(ql, lineEditmountname);
  
  vdevices->addWidget(qb);
  
  ui->tabdevices->setLayout(vdevices);
  
  connect(ui->pushButtonupdatejoypaddevices, SIGNAL(clicked(bool)), this, SLOT(updateJoyDevs()));
}

void HCSettings::accept()
{
  hApp->getHandController()->setServerName(lineEdithostname->text());
  hApp->getHandController()->setPortString(lineEdithostport->text());
  hApp->getHandController()->setMountName(lineEditmountname->text());
  
  lm->le->close();
  
  QDialog::accept();
}


int HCSettings::exec(bool triggered)
{
  if (available.count() == 0) {
    ui->scrollAreajoypads->setWidget(labelnojoypad);
    ui->checkBoxautoconnectjoypad->setEnabled(false);
  } else {
    if(not lm->le) {
      //otherwise, make a new LayoutEdit dialog and show it.
      lm->le = new LayoutEdit(lm);
      //lm->le->setParent(ui->scrollAreajoypadsContents);
      ui->scrollAreajoypads->setWidget(lm->le);
      lm->le->setLayout(lm->CurrentLayout);
     }
     ui->checkBoxautoconnectjoypad->setEnabled(true);
  }
  return QDialog::exec();
}

void HCSettings::updateJoyDevs()
{
  lm->updateJoyDevs();
  if (available.count() == 0) {
    ui->scrollAreajoypads->setWidget(labelnojoypad);
    ui->checkBoxautoconnectjoypad->setEnabled(false);
  } else {
    if(not lm->le) {  
      //otherwise, make a new LayoutEdit dialog and show it.
      lm->le = new LayoutEdit(lm);
      //lm->le->setParent(ui->scrollAreajoypadsContents);
      ui->scrollAreajoypads->setWidget(lm->le);
      lm->le->setLayout(lm->CurrentLayout);
    } else {
      lm->le->updateJoypadWidgets(); 
    }
    ui->checkBoxautoconnectjoypad->setEnabled(false);
  }
}

LayoutManager* HCSettings::getLayoutManager()
{
  return lm;
}
